package bancosys.tec.security.impl.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Restrictions;

import bancosys.tec.persist.dao.BaseDAO;
import bancosys.tec.security.impl.domain.Credential;

/**
 * {@link BaseDAO} responsavel por buscar {@link Credential}
 * 
 * @param <T> tipo do DAO
 */
public abstract class AbstractCredentialDAO<T extends Credential> extends BaseDAO<T> {

    /**
     * Monta uma {@link Criteria} levando em consideracao o {@link Credential#getClient()} usado
     * 
     * @param exampleInstance Credential
     * @param literal boolean
     * @param excludeProperties propriedades a serem excluidas
     * @return {@link List}
     * @deprecated never use find by example
     */
    @Deprecated
    @Override
    public List<T> findByExample(T exampleInstance, boolean literal, String... excludeProperties) {
        if (exampleInstance == null) {
            return new ArrayList<T>();
        }
        Criteria crit = this.getSession().createCriteria(this.getPersistableClass());
        Example ex = this.mountExample(exampleInstance, literal, excludeProperties);
        crit.add(ex);
        if (exampleInstance.getClient() != null) {
            crit = crit.createCriteria("client", "c").add(Restrictions.eq("c.name", exampleInstance.getClient().getName()));
            crit.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        }
        return this.executeQuery(crit);
    }
}
